/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.github.xwc.view.util;

import ohos.app.Context;
import ohos.global.resource.NotExistException;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.media.image.ImageSource;
import ohos.media.image.PixelMap;
import ohos.media.image.common.PixelFormat;
import ohos.media.image.common.Rect;
import ohos.media.image.common.Size;

import java.io.IOException;
import java.io.InputStream;

/**
 * Description:类似 BitmapFactory.decodeResource 获取 Bitmap
 * ImageSource.DecodingOptions可以自己按需求添加其他属性
 *
 * @author renpengfei
 * @since 2021-03-18
 */
public class PixelMapFactoryUtil {
    private static final HiLogLabel LABLE = new HiLogLabel(HiLog.LOG_APP, 0x00200, "PixelMapFactoryUtil");

    private static final String IMAGE_PNG = "image/png";
    private static PixelMapFactoryUtil instance = null;

    private PixelMapFactoryUtil() {
    }

    /**
     * 静态锁对象
     *
     * @return Utils
     */
    public static synchronized PixelMapFactoryUtil getInstance() {
        if (instance == null) {
            instance = new PixelMapFactoryUtil();
        }
        return instance;
    }
    /**
     * 获取解码属性
     *
     * @return ImageSource
     */
    public static ImageSource.DecodingOptions getDecodingOptions() {
        ImageSource.DecodingOptions decodingOptions = new ImageSource.DecodingOptions();
        decodingOptions.desiredSize = new Size(0, 0);
        decodingOptions.desiredRegion = new Rect(0, 0, 0, 0);
        decodingOptions.desiredPixelFormat = PixelFormat.ARGB_8888;

        return decodingOptions;
    }

    /**
     * 根据资源文件获取位图
     *
     * @param context
     * @param resId
     * @param decodingOptions
     * @return PixelMap
     */
    public static PixelMap getPixelMapFromResource(Context context, int resId,
            ImageSource.DecodingOptions decodingOptions) {
        PixelMap pixelmap = null;
        InputStream drawableInputStream = null;
        try {
            drawableInputStream = context.getResourceManager().getResource(resId);
            ImageSource.SourceOptions sourceOptions = new ImageSource.SourceOptions();
            sourceOptions.formatHint = IMAGE_PNG;
            ImageSource imageSource = ImageSource.create(drawableInputStream, sourceOptions);
            pixelmap = imageSource.createPixelmap(decodingOptions);
            return pixelmap;
        } catch (NotExistException ex) {
            HiLog.error(LABLE, ex + "");
        } catch (IOException ex) {
            HiLog.error(LABLE, ex + "");
        } finally {
            try {
                if (drawableInputStream != null) {
                    drawableInputStream.close();
                }
            } catch (IOException ex) {
                HiLog.error(LABLE, ex + "");
            }
        }
        return pixelmap;
    }


}
